cmake_minimum_required(VERSION 3.16)

project(hiapp-lite VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Quick Core Widgets Sql Network WebSockets)

qt_standard_project_setup(REQUIRES 6.7)

# 查找 libuv
# 优先查找 third_party/libuv/x64-windows 目录（vcpkg 格式）
find_path(LIBUV_INCLUDE_DIR uv.h
    PATHS
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/include
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/include
        ${CMAKE_SOURCE_DIR}/third_party/libuv/include
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/include
        /usr/local/include
        /usr/include
        $ENV{LIBUV_DIR}/include
        $ENV{LIBUV_ROOT}/include
)

find_library(LIBUV_LIBRARY 
    NAMES uv libuv uv_a
    PATHS
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/lib
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/debug/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/debug/lib
        ${CMAKE_SOURCE_DIR}/third_party/libuv/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/lib
        /usr/local/lib
        /usr/lib
        $ENV{LIBUV_DIR}/lib
        $ENV{LIBUV_ROOT}/lib
)

if(NOT LIBUV_INCLUDE_DIR OR NOT LIBUV_LIBRARY)
    message(FATAL_ERROR 
        "libuv not found!\n"
        "Please ensure libuv is installed in:\n"
        "  - ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows\n"
        "  Or set LIBUV_DIR environment variable pointing to libuv installation directory."
    )
endif()

message(STATUS "Found libuv:")
message(STATUS "  Include dir: ${LIBUV_INCLUDE_DIR}")
message(STATUS "  Library: ${LIBUV_LIBRARY}")



qt_add_executable(HiAPP-lite
    main.cpp
    # database_example.cpp
    runtimefilesystemwatcher.cpp
    databasemanager.cpp
    backendservice.cpp
    websocketserver.cpp
    systemtraymanager.cpp
    HiAPP-lite.rc
)


# 添加资源文件
qt_add_resources(HiAPP-lite "icons"
    PREFIX "/"
    FILES
        icons/logo.ico
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(HiAPP-lite PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.HiAPP-lite
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(HiAPP-lite
    PRIVATE Qt6::Quick Qt6::Core Qt6::Widgets Qt6::Sql Qt6::Network Qt6::WebSockets
)

# 链接 libuv
target_link_libraries(HiAPP-lite PRIVATE ${LIBUV_LIBRARY})
target_include_directories(HiAPP-lite PRIVATE ${LIBUV_INCLUDE_DIR})


# Windows API 支持
if(WIN32)
    target_link_libraries(HiAPP-lite
        PRIVATE shell32 shlwapi comctl32
    )
endif()

include(GNUInstallDirs)
install(TARGETS HiAPP-lite
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
